//
//  ZYXWebViewDemoViewController.m
//  Internet
//
//  WebView Demo
//

#import "ZYXWebViewDemoViewController.h"

@interface ZYXWebViewDemoViewController () <UIWebViewDelegate>

@property (weak, nonatomic) IBOutlet UIWebView *webView;

- (IBAction)clearAd;
- (IBAction)getHtmlTitle;

@end

@implementation ZYXWebViewDemoViewController


- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor redColor];
    
    // NSURL *url = [[NSBundle mainBundle] URLForResource:@"deal.html" withExtension:nil];
    // NSURL *url = [[NSBundle mainBundle] URLForResource:@"iOS 7 Cookbook.pdf" withExtension:nil];
    // NSURL *url = [[NSBundle mainBundle] URLForResource:@"minion.mp4" withExtension:nil];
    NSURL *url = [[NSBundle mainBundle] URLForResource:@"test.html" withExtension:nil];
    // NSURL *url = [[NSBundle mainBundle] URLForResource:@"关于.txt" withExtension:nil];
    // NSURL *url = [[NSBundle mainBundle] URLForResource:@"华为编程规范.doc" withExtension:nil];
    // NSURL *url = [[NSBundle mainBundle] URLForResource:@"网络基础.pptx" withExtension:nil];
    
    
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    
    // 设置需要检测的数据类型
    self.webView.dataDetectorTypes = UIDataDetectorTypeAll;
    self.webView.delegate = self;
    // self.webView.scalesPageToFit = YES;
    [self.webView loadRequest:request];
}


#pragma mark - UIWebViewDelegate

/**
 * 当webView发送一个请求之前都会调用这个方法, 返回YES, 可以加载这个请求, 返回NO, 代表禁止加载这个请求
 */
- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType
{
    // ZYXLog(@"webView:shouldStartLoadWithRequest:navigationType:");
    
    // 2015-04-05 13:09:51.229 Internet[58562:1177804] webView:shouldStartLoadWithRequest:navigationType:
    
    NSString *url = request.URL.absoluteString;
    
    ZYXLog(@"%@",url);
    
    // 2015-04-05 13:33:46.499 Internet[60556:1230138]
    // file:///Users/zhaoyingxin/Library/Developer/CoreSimulator/Devices/E92F8A62-B239-4ADA-945B-D8028C94FD5C/data/Containers/Bundle/Application/2F2DEFEE-020B-4BE0-94E1-11A0B4A10C90/Internet.app/deal.html
    

    //    file:///Users/zhaoyingxin/Library/Developer/CoreSimulator/Devices/E92F8A62-B239-4ADA-945B-D8028C94FD5C/data/Containers/Bundle/Application/9AFA9D99-3EEE-4951-BF93-013FAFB27480/Internet.app/test.html

    //    ios://openWeibo
    
    NSRange range = [url rangeOfString:@"ios://"];
    
    if (range.length != 0) {
        // 截取方法名
        NSString *method = [url substringFromIndex:range.location + range.length];
        // 将方法名转为SEL类型
        //ZYXLog(@"%@",method); // ios://openWeibo --> openWeibo
        
        SEL selector = NSSelectorFromString(method);
        [self performSelector:selector withObject:nil];
    }
    return YES;
}


- (IBAction)clearAd {
    // 清除广告
    // 执行JavaScript代码, 把header和footer给删掉
    // document.getElementsByTagName('header')[0].remove();
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('header')[0].remove();"];
    [self.webView stringByEvaluatingJavaScriptFromString:@"document.getElementsByTagName('footer')[0].remove();"];
    
    // HTML5 : 跨平台(网页) JavaScript
    // PhoneGap
    // Sencha-touch
    // jQuery-Mobile
}


- (IBAction)getHtmlTitle {
    NSString *title = [self.webView stringByEvaluatingJavaScriptFromString:@"document.title;"];
    ZYXLog(@"%@", title);
    // 2015-04-05 13:14:40.456 Internet[59142:1188412] solozyx测试网页
    // 2015-04-05 13:53:27.947 Internet[61864:1271416] 万岁寿司·料理代金券团购详情-大众点评团广州站
}


- (void)openMyAlbum
{
    ZYXLog(@"openMyAlbum");
    
    UIImagePickerController *ipc = [[UIImagePickerController alloc] init];
    ipc.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    [self presentViewController:ipc animated:YES completion:nil];
}

- (void)openMyCamera
{
    ZYXLog(@"openMyCamera");
}

- (void)openWeibo
{
    ZYXLog(@"openWeibo");
}

/*
    http://blog.csdn.net/like7xiaoben/article/details/7815168
 
    NSString *str1 = @"can you \n speak English";
    NSString *str  = @"\n";
    //在str1这个字符串中搜索\n，判断有没有
    if ([str1 rangeOfString:str].location != NSNotFound) {
        NSLog(@"这个字符串中有\n");
    }
    //rangeOfString 前面的参数是要被搜索的字符串，后面的是要搜索的字符
    //NSNotFound 表示请求操作的某个内容或者item没有发现，或者不存在
*/

@end
